﻿using A.M.ETO;
using A.M.Storage.Application;
using A.M.Storage.I_Infrastruecure;
using L.M.System.Application.IServices;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.IRepository;
using L.M.System.Infrastruecure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace L.M.System.Application.Services
{
    /// <summary>
    /// 角色和一级权限
    /// </summary>
    public class Role_permission_tableServices : BaseServices<role_permission_table, string>, IRole_permission_tableServices
    {
        private readonly IRole_permission_tableRepository role_Permission_TableRepository;
        private readonly IRole_authorit_listServices role_Authorit_ListServices;
        private readonly IAuthority_listServices authority_ListServices;
        private readonly IAuthority_list_inforServices authority_List_InforServices;

        public Role_permission_tableServices(IRole_permission_tableRepository role_Permission_TableRepository, IRole_authorit_listServices role_Authorit_ListServices, IAuthority_listServices authority_ListServices, IAuthority_list_inforServices authority_List_InforServices = null) : base(role_Permission_TableRepository)
        {
            this.role_Permission_TableRepository = role_Permission_TableRepository;
            this.role_Authorit_ListServices = role_Authorit_ListServices;
            this.authority_ListServices = authority_ListServices;
            this.authority_List_InforServices = authority_List_InforServices;
        }

        /// <summary>
        /// 反填该角色下的一级权限
        /// </summary>
        /// <param name="rId">角色编号</param>
        /// <returns>反填该角色下的一级权限</returns>
        public async Task<ApiReturn<string>> GetRome_OneInfor(string? rId)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            string? oinfor = "";
            var list = (await role_Permission_TableRepository.GetShowQueryableAsync(c => c.role_number == rId)).ToList();
            if (list.Count() != 0)
            {
                foreach (var item in list)
                {
                    oinfor += item.authority_number + ",";
                }
            }
            apiReturn.Code = 200;
            apiReturn.Message = oinfor.TrimEnd(',');
            return apiReturn;
        }

        /// <summary>
        /// 反填第二级权限
        /// </summary>
        /// <param name="rId">角色编号</param>
        /// <returns>反填第二级权限</returns>
        public async Task<ApiReturn<string>> GetRole_TwoInfor(string? rId)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var list = (await role_Authorit_ListServices.GetShowQueryableAsync(c => c.role_number == rId)).ToList();
            string[] arr = new string[list.Count()];
            var index = 0;
            foreach (var item in list)
            {
                arr[index] = item.aid;
                index++;
            }
            var aulist = authority_ListServices.GetShowInfor();
            List<authority_list_infor> rlist = new List<authority_list_infor>();
            foreach (var item in aulist)
            {
                rlist.AddRange(authority_List_InforServices.GetShowInfor(c=>c.fid == item.authority_numbe));
            }
            
            string[] err = new string[rlist.Count()];
            var index1 = 0;
            foreach (var item in rlist)
            {
                err[index1] = item.aid;
                index1++;
            }

            var commonItems = arr.Intersect(err);

            //定义第一级编号
            string oneInfor = "";
            foreach (var item in commonItems)
            {
                oneInfor += item + ",";
            }

            var differentItemsArray2 = arr.Except(err);
            //定义第二级编号
            string twoInfor ="" ;
            foreach (var item in differentItemsArray2)
            {
                twoInfor += item + ",";
            }

            var mach = oneInfor.TrimEnd(',') + "_" + twoInfor.TrimEnd(',');

            apiReturn.Code = 200;
            apiReturn.Message = mach;
            return apiReturn;
        }
    }
}
